package sys.decision;

import java.io.*;

public class DecisionSubsystem
{
    public DecisionSubsystem(int allNetTrainingLevel, int oneNodeTrainingLevel)
    {
        try 
        {
            String line;
            /*p = Runtime.getRuntime().*/
                //exec("ruby -C /home/chigorin/botva/term10/my/ds/cw/distsyst/sysb/src/decision teacher.rb " + 
                    /*allNetTrainingLevel + " " + oneNodeTrainingLevel + " listen");*/

            p = Runtime.getRuntime().
                exec("ruby -C ../src/decision teacher.rb " + 
                    allNetTrainingLevel + " " + oneNodeTrainingLevel + " listen");

            stdin = p.getOutputStream ();
            stdout = p.getInputStream ();

            output = new BufferedReader(new InputStreamReader(p.getInputStream()));
        }
        catch (Exception e) 
        {
            System.out.println("Error when constructing decision system : " + e.getMessage());
        }
    }

    public void destroy()
    {
        try
        {
            stdin.close();
            stdout.close();
            output.close();
            p.destroy();
        }
        catch(Exception e)
        {
            System.out.println("Error when closing streams : " + e.getMessage());
        } 
    }

    public void newUser(String system, String user, String prolog)
    {
        String line = null;

        try
        {
            line = "newUser\n";   
            stdin.write(line.getBytes() );
            stdin.flush();

            line = system + " " + user + " " + prolog + "\n";   
            stdin.write(line.getBytes() );
            stdin.flush();
            
            line = output.readLine();
        }
        catch(Exception e)
        {
            System.out.println("Error when adding a new user : " + e.getMessage());
            return;
        }
        System.out.println("Adding new user : " + line);
    }

    public int decide(String system, String user, int quantity, int percent)
    {
        String line = null;

        try
        {
            line = "activate\n";   
            stdin.write(line.getBytes() );
            stdin.flush();

            line = system + " " + user + " " + quantity + " " +  percent + "\n";   
            stdin.write(line.getBytes() );
            stdin.flush();

            line = output.readLine();
        }
        catch(Exception e)
        {
            System.out.println("Error when making a decision : " + e.getMessage());
            return 0;
        }
        System.out.println(Float.valueOf(line));
        return Math.round(Float.valueOf(line));
    }

    public void teach(String system, String user, int quantity, int percent, int decision)
    {
        String line = null;

        try
        {
            line = "teach\n";   
            stdin.write(line.getBytes() );
            stdin.flush();

            line = system + " " + user + " " + quantity + " " +  percent + " " + decision + "\n";   
            stdin.write(line.getBytes() );
            stdin.flush();

            line = output.readLine();
        }
        catch(Exception e)
        {
            System.out.println("Error when teaching the network : " + e.getMessage());
            return;
        }
        System.out.println("Teaching network : " + line);
    }

    private Process p;
    private BufferedReader output;

    OutputStream stdin;
    InputStream stdout;
}
